package com.chen.mongodb;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author chenyulei
 */
@Service
public class BookServiceImpl implements BookService{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public List<Book> findAll() {
        return mongoTemplate.findAll(Book.class);
    }

    @Override
    public Book findById(Integer bookId) {
        return mongoTemplate.findById(bookId,Book.class);
    }

    @Override
    public Book save(Book book) {
        mongoTemplate.save(book);
        return book;
    }

    @Override
    public void deleteById(Integer bookId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(bookId));
        mongoTemplate.remove(query,Book.class);
    }
}
